package top.tocchen.bookshop.controller;

import top.tocchen.bookshop.entity.User;
import top.tocchen.bookshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author tocchen
 * @date 2022/8/9 19:34
 * @since jdk 1.8
 **/
@Controller
public class LoginController {
    @Autowired
    UserService userService;
    @RequestMapping("/toLogin")
    public String goLogin(){
        return "login";
    }

    @RequestMapping("/login")
    @ResponseBody
    public String runlogin(User record, HttpSession session){
        User result = userService.findByNameAndPassword(record);
        if(result!=null){
            session.setAttribute("user_name",result.getName());
            session.setAttribute("user_id",result.getId());
            return "300";
        }else{
            return "250";
        }
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "index";
    }
}
